package com.mp3player.service;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;

import com.mp3player.download.HttpDownloader;
import com.mp3player.model.Mp3Info;
import com.mp3player.mp3player.R;
import com.mp3player.util.MetaData;

public class DownloadService extends Service {

    public DownloadService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Mp3Info mp3Info = null;
        try {
            mp3Info = (Mp3Info) intent.getSerializableExtra(MetaData.MP3_INFO_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(mp3Info);
        DownloadThread downloadThread = new DownloadThread(mp3Info);
        Thread thread = new Thread(downloadThread);
        thread.start();
//        return super.onStartCommand(intent, flags, startId);
        return Service.START_NOT_STICKY;
    }

    /**
     * 用于下载文件的线程
     */
    class DownloadThread implements Runnable {
        private Mp3Info mp3Info = null;
        public DownloadThread(Mp3Info mp3Info) {
            this.mp3Info = mp3Info;
        }

        @Override
        public void run() {
            //获得mp3文件下载路径
            String mp3UrlStr = MetaData.getMusicUrl() + mp3Info.getMp3Name();
            String lrcUrlStr = MetaData.getLrcUrl() + mp3Info.getLrcName();

            //下载文件并获取下载结果
            HttpDownloader httpDownloader = new HttpDownloader();
            int mp3_return_status = httpDownloader.downloadBinaryFile(mp3UrlStr, MetaData.SAVE_DIR, mp3Info.getMp3Name());
            int lrc_return_status = httpDownloader.downloadBinaryFile(lrcUrlStr, MetaData.SAVE_DIR, mp3Info.getLrcName());

            //如果mp3和lrc都下载成功，则下载结果为0，否则为其他结果
            int return_status = -1;
            if (mp3_return_status == 0 && lrc_return_status == 0) {
                return_status = 0;
            } else if (mp3_return_status == 1 && lrc_return_status == 1) {
                return_status = 1;
            }
            System.out.println("下载结果："+return_status);

            //根据下载结果作出相应的通知
            if (0 == return_status) {
                postDownloadFinishNotification();
            } else if (-1 == return_status) {
                postDownloadErrorNotification();
            } else if (1 == return_status) {
//                Toast.makeText(DownloadService.this, DownloadService.this.getText(R.string.file_exist_warning), Toast.LENGTH_SHORT).show();
            }
        }

        /**
         * 产生下载完成的通知
         */
        private void postDownloadFinishNotification() {
            NotificationManager notificationManager = (NotificationManager) DownloadService.this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(DownloadService.this);
            builder.setSmallIcon(R.mipmap.ic_launcher_round)
                    .setContentTitle(MetaData.DOWNLOAD_FINISH)
                    .setContentText(mp3Info.getMp3Name()+MetaData.DOWNLOAD_FINISH);
            notificationManager.notify(1, builder.build());
        }

        /**
         * 产生下载出错的通知
         */
        private void postDownloadErrorNotification() {
            NotificationManager notificationManager = (NotificationManager) DownloadService.this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(DownloadService.this);
            builder.setSmallIcon(R.mipmap.ic_launcher_round)
                    .setContentTitle(MetaData.DOWNLOAD_ERROR)
                    .setContentText(mp3Info.getMp3Name()+MetaData.DOWNLOAD_ERROR);
            notificationManager.notify(1, builder.build());
        }
    }
}
